package com.ql.dev.customwebview.demo;

import android.app.Activity;
import android.text.Html;
import android.webkit.WebView;

import com.ql.dev.customwebview.library.views.HostJsScope;
import com.ql.dev.customwebview.library.views.JsCallback;
import com.ql.dev.customwebview.utils.ClipboardUtils;

/**
 * 必须在注入类中定义为public static且第一个参数接收WebView，其他参数的类型可以是int、long、double、boolean、String、JSONObject、JsCallback
 * Created by Administrator on 2018-10-27 0027.
 */
public class CustomHostJsScope extends HostJsScope{

    /**
     * 打印网页面内部源码
     * @param webView
     * @param html
     */
    public void showSource(WebView webView, String html) {

    }

    /**
     * 需要关闭该页面
     * @param view
     */
    public static void backToChat(WebView view){
        ((Activity)view.getContext()).finish();
    }

    /**
     * 复制内容到剪切板
     * @param view
     */
    public static void copyToAndroid(WebView view,String copyContent){
        ClipboardUtils.clipboardCopyText(view.getContext(), Html.fromHtml(copyContent));
        //Toast.makeText(view.getContext(),"成功复制到剪切板",Toast.LENGTH_LONG).show();
    }

    /**
     * Js回调方法
     * @param view 必要的参数
     * @param backMsg
     * @param jsCallback  function (msg) {android.methodName(msg);}
     */
    public static void doJsCallBack(WebView view,final String backMsg, final JsCallback jsCallback) {
        try {
            jsCallback.apply(backMsg);
        } catch (JsCallback.JsCallbackException e) {
            e.printStackTrace();
        }
    }
}
